require(tidyverse)

rm(list = ls())
gc()

################################################################################
##
## Date:    2024-12-20
## Author:  james.h.bisbee@vanderbilt.edu
## Purpose: This script generates Figure 5.
## Inputs:  /scratch/jhb362/zilinsky_2023/data/results/VIMP_ranger/VIMP_2024_outcome-.*(ECON|ENOUGHMON).*-months.*FALSE_temp-chg12.RData
##            - Variable importance results generated by NFR_vimp_prep.R
##            - Summarized on the NYU HPC into PSRM_comb_months_ECON_ENOUGHMON.RData via NFR_data_prep.R
## Outputs: ./figures/fig5.pdf
##
################################################################################

# Compute details
print(paste0('Compute environment from ',Sys.Date(),' run by Bisbee'))
if(Sys.info()['sysname'] == 'Windows') {
  ram_size = system("wmic MemoryChip get Capacity", intern = TRUE)[-1]
  model_name = system("wmic cpu get name", intern = TRUE)[2] # nocov
  vendor_id = system("wmic cpu get manufacturer", intern = TRUE)[2] # nocov
  
  print(list(ram = stringr::str_squish(ram_size)[1],
             vendor_id = stringr::str_squish(vendor_id),
             model_name = stringr::str_squish(model_name),
             no_of_cores = parallel::detectCores()))
} else if(Sys.info()['sysname'] == 'Linuxs') {
  splitted <- strsplit(system("ps -C rsession -o %cpu,%mem,pid,cmd", intern = TRUE), " ")
  df <- do.call(rbind, lapply(splitted[-1], 
                              function(x) data.frame(
                                cpu = as.numeric(x[2]),
                                mem = as.numeric(x[4]),
                                pid = as.numeric(x[5]),
                                cmd = paste(x[-c(1:5)], collapse = " "))))
  df
} else {
  cat("If not on Linux or Windows, you'll have to figure out your own solution to seeing the compute environment.")
}

sessionInfo()

load('./data/VIMP_ranger/PSRM_comb_months_ECON_ENOUGHMON.RData')


lookup <- vimp %>%
  count(outcome)

lookup$labs <- c("How would you rate economic\nconditions in this country today?",
                 "[BINARY] How would you rate economic\nconditions in this country today?",
                 "Have there been times in the past twelve months when you did not\nhave enough money to buy food that you or your family needed?",
                 "Right now, do you think that economic conditions in this\ncountry, as a whole, are getting better or getting worse?",
                 "[BINARY] Right now, do you think that economic conditions in this\ncountry, as a whole, are getting better or getting worse?")


toplot <- vimp %>%
  mutate(relVimp = vimp / predErr) %>%
  select(vars,outcome,period,n,relVimp) %>%
  group_by(vars,outcome,period) %>%
  summarise_all(list(mean = ~mean(.,na.rm=T),
                     lb = ~quantile(.,.005,na.rm=T),
                     ub = ~quantile(.,.995,na.rm=T))) %>%
  ungroup() %>%
  left_join(lookup)

pdf('./figures/fig5.pdf',width = 7,height = 5)
toplot %>%
  mutate(period = as.Date(period)) %>%
  filter(grepl('PARTY',vars)) %>%
  filter(outcome %in% c('ENOUGHMON','ECON')) %>%
  ggplot(aes(x = period,y = relVimp_mean,color = outcome)) + #,size = n_mean)) + 
  annotate('rect',xmin = as.Date('2008-11-04'),ymin = -Inf,xmax = as.Date('2009-01-20'),ymax = Inf,fill = 'black',alpha = .1,color = 'grey70') +
  annotate('rect',xmin = as.Date('2012-11-06'),ymin = -Inf,xmax = as.Date('2013-01-20'),ymax = Inf,fill = 'black',alpha = .1,color = 'grey70') + 
  annotate('rect',xmin = as.Date('2016-11-08'),ymin = -Inf,xmax = as.Date('2017-01-20'),ymax = Inf,fill = 'black',alpha = .1,color = 'grey70') + 
  geom_errorbar(aes(ymin = relVimp_lb,ymax = relVimp_ub),size = .5) + 
  # geom_point(shape = 21,fill = 'white') + 
  geom_point() + 
  theme_bw() +
  scale_y_continuous(labels = scales::percent) + 
  scale_color_manual(values = c('ECON' = 'orange','ENOUGHMON' = 'grey40'),
                     labels = c('ECON' = 'How would you rate economic\nconditions in this country today?',
                                'ENOUGHMON' = 'Enough money to buy food\nthat you or your family needed?')) +
  labs(x = 'Month',
       y = '% Reduction in error',
       title = 'Variable importance: party ID over time',
       subtitle = 'Permutation tests using random forests',
       size = '# Responding',
       color = 'Outcome') + 
  # facet_wrap(~outcome,scales = 'free') +
  guides(color=guide_legend(
    keywidth=0.2,
    keyheight=0.4,
    default.unit="inch")
  ) + 
  theme(legend.position = 'bottom')
dev.off()

# EOF